# 废稿

# Matplot绘图模块
import matplotlib.pyplot as plt
from line import Line
from point import Point

class ui():
  def __init__(self):
    ui.fig = plt.figure(figsize=(800,600))
    ui.scale = 10
    ui.lines = []
    plt.ion()
    plt.show()
    
  @classmethod
  def set_scale(self,length = None):
    if(length):self.scale = length
    plt.xlim(-self.scale,self.scale)
    plt.ylim(-self.scale,self.scale)

  @classmethod
  def draw_line(self,point1:Point,point2:Point):
    self.lines.append(Line(point1,point2))
    plt.plot((point1.x,point2.x),(point1.y,point2.y),marker = 'o',markerfacecolor='r',markersize = 10)
    self.fig.canvas.draw_idle()
    # plt.ioff()
  
  @classmethod
  def clean(self):
    plt.cla()
    self.lines = []
    self.set_scale()
  

  @classmethod
  def pause(self,t):
    plt.pause(t)


if __name__=='__main__':
  p = ui()
  plt.waitforbuttonpress()

# QT绘图模块
import sys
from PyQt5.QtCore import Qt,QPoint,QTimer
from PyQt5.QtGui import QPainter, QBrush, QPixmap,QColor
from PyQt5.QtWidgets import QApplication, QWidget
import time

import numpy as np
import sympy as sp  # 实现求导
from sympy.abc import t # 引入时间t的符号，等价于t = sp.symbols('t')
import matplotlib.pyplot as plt

from line import Line
from point import Point
from rob import Rob

class ui(QWidget):
  def __init__(self):
    super(ui, self).__init__()
    self.resize(800,800)
    self.brush = QBrush(Qt.SolidPattern)
    self.red_brush = QBrush(QColor(255,0,0),Qt.SolidPattern)
    self.h = 800
    self.w = 800

    # 时间模块
    # self.timer = QTimer(self)
    # self.timer.timeout.connect(self.delay)

    self.lines = []


  def draw_line(self,point1:Point,point2:Point):
    # print(point1,point2,self.lines)
    self.lines.append((QPoint(point1.x,point1.y),QPoint(point2.x,point2.y)))
    self.update()

  def pause(self,s):
    # self.timer.start(ms)
    time.sleep(s)


  def paintEvent(self, QPaintEvent):
    p = QPainter(self)
    p.setBrush(self.brush)
    p.begin(self)

    if(len(self.lines)>0):
      for line in self.lines:
        p.drawLine(line[0],line[1])
      
      p.setBrush(self.red_brush)
      for line in self.lines:
        p.drawPoint(line[0])
        p.drawPoint(line[1])
    else:
      p.drawText(100,100,'无数据')

    p.end()


if __name__ == '__main__':
  app = QApplication(sys.argv)
  window = ui()
  window.show()

  r = Rob(300,Point(0,0),sp.cos(t))
  interval = 0.5
  for t in np.arange(0,5,interval):
    r.update(t)
    window.draw_line(r.basePoint,r.endPoint)
    window.pause(interval)


  sys.exit(app.exec_())



# 缓存式绘图
import numpy as np
import pyqtgraph as pg
import time
import sys
import array
from rob import Rob

# 创建窗口
app = pg.mkQApp()  # 建立app
win = pg.GraphicsWindow()  # 建立窗口
win.setWindowTitle(u'pyqtgraph 实时波形显示工具')
win.resize(800, 500)  # 小窗口大小
# 创建图表
historyLength = 100  # 横坐标长度
p = win.addPlot()  # 把图p加入到窗口中
p.showGrid(x=True, y=True)  # 把X和Y的表格打开
p.setRange(xRange=[0, historyLength], yRange=[0, 100], padding=0)
  

robs = []
cuvers = []
# 三维数组：每维度依次存储：杆件、杆件在不同时间的位置，某时刻杆件端点坐标x1,y1,x2,y2
def draw_line(rob:Rob,index,clean = True):
  """
  l:元组,x1,y1,x2,y2,x3
  index : 杆件编号
  clean :是否保留轨迹
  """
  assert index<=len(robs)
  # 增加新杆件
  if(index==len(robs)):
    robs.append([])
    if(clean):
      # 起点到每个终点都需要一个画笔
      cuvers.append([p.plot() for i in range(rob.endPoint_num)])
  # 更新杆件位置
  robs[index].append(rob.copy())
  

def start_draw(interval_ms):
  timer = pg.QtCore.QTimer()
  timer.timeout.connect(flush)#定时调用flush函数
  timer.start(interval_ms)#多少ms调用一次
  finish()

def flush():
  index = -1
  for arob in robs:
    index+=1
    if(len(arob)>0):
      # r:某一时刻的杆件
      r = arob.pop()
      # 画杆件的每一根线
      for i in range(r.endPoint_num):
        cuvers[r.index][i].setData((r.basePoint.x,r.endPoints[i].x),(r.basePoint.y,r.endPoints[i].y))
    

def finish():
  sys.exit(app.exec_())

## Start Qt event loop unless running in interactive mode or using pyside.
if __name__ == '__main__':
  import sys
  if sys.flags.interactive != 1 or not hasattr(pg.QtCore, 'PYQT_VERSION'):
    pg.QtGui.QApplication.exec_()

